package com.yanqu.road.dao.impl.activity.sonhai;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.sonhai.data.*;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class SonHaiUserDataDaoImpl extends TempDataObjectDao<SonHaiUserData> {

    public static String getSonHaiUserDataSql = "select * from `t_c_activity_son_hai_user_data` where `activityId` = ? and groupId = ?";

    public Map<Long, SonHaiUserData> getSonHaiUserDataMap(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Long, SonHaiUserData> map = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCrossDb());
            call = getDb().getConnection().prepareCall(getSonHaiUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                SonHaiUserData data = parse(rs);
                map.put(data.getUserId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String updateSonHaiUserDataSql = "update `t_c_activity_son_hai_user_data` set teamId=?,serverId=?,energy=?,energyRestoreTime=?,score=?,auraScore=?,buffComeNum=?,nextBuffComeNeedEnergy=?,waitSelectBuffList=?,buffMap=?,regionId=?,x=?,y=?,pvpKillNum=?,pvpCanBeAttNum=?,dieFlag=?,dieData=?,talentAddition=?,power=?,allBlood=?,reBlood=?,reBloodInfoMap=?,selectRegionFlag=?,helpNum=?,helpNumCount=?,language=?,createTime=? where activityId=? and groupId = ? and userId=?";
    @Override
    protected boolean updateData(SonHaiUserData sonHaiUserData) {
        sonHaiUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getEnergyRestoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getAuraScore()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getBuffComeNum()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getNextBuffComeNeedEnergy()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiUserData.getWaitSelectBuffList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiUserData.getBuffMap())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getX()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getY()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getPvpKillNum()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getPvpCanBeAttNum()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getDieFlag()));
        String dieData = "";
        if (sonHaiUserData.getDieData() != null) {
            dieData = JSON.toJSONString(sonHaiUserData.getDieData());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, dieData));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getTalentAddition()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getAllBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getReBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiUserData.getReBloodInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getSelectRegionFlag()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getHelpNum()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getHelpNumCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, sonHaiUserData.getLanguage()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getCreateTime()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getUserId()));
        if (getDb().executeNoneQuery(updateSonHaiUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSonHaiUserDataSql = "insert into `t_c_activity_son_hai_user_data`(activityId,groupId,userId,teamId,serverId,energy,energyRestoreTime,score,auraScore,buffComeNum,nextBuffComeNeedEnergy,waitSelectBuffList,buffMap,regionId,x,y,pvpKillNum,pvpCanBeAttNum,dieFlag,dieData,talentAddition,power,allBlood,reBlood,reBloodInfoMap,selectRegionFlag,helpNum,helpNumCount,language,createTime) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(SonHaiUserData sonHaiUserData) {
        sonHaiUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCrossDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getGroupId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getServerId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getEnergyRestoreTime()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getScore()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getAuraScore()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getBuffComeNum()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getNextBuffComeNeedEnergy()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiUserData.getWaitSelectBuffList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiUserData.getBuffMap())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getRegionId()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getX()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getY()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getPvpKillNum()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getPvpCanBeAttNum()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getDieFlag()));
        String dieData = "";
        if (sonHaiUserData.getDieData() != null) {
            dieData = JSON.toJSONString(sonHaiUserData.getDieData());
        }
        para.put(i++, new DbParameter(Types.VARCHAR, dieData));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getTalentAddition()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getPower()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getAllBlood()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getReBlood()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(sonHaiUserData.getReBloodInfoMap())));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getSelectRegionFlag()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getHelpNum()));
        para.put(i++, new DbParameter(Types.INTEGER, sonHaiUserData.getHelpNumCount()));
        para.put(i++, new DbParameter(Types.VARCHAR, sonHaiUserData.getLanguage()));
        para.put(i++, new DbParameter(Types.BIGINT, sonHaiUserData.getCreateTime()));
        if (getDb().executeNoneQuery(insertSonHaiUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SonHaiUserData parseData(ResultSet rs) throws SQLException {
        SonHaiUserData sonHaiUserData = new SonHaiUserData();
        sonHaiUserData.setActivityId(rs.getInt("activityId"));
        sonHaiUserData.setGroupId(rs.getInt("groupId"));
        sonHaiUserData.setUserId(rs.getLong("userId"));
        sonHaiUserData.setTeamId(rs.getInt("teamId"));
        sonHaiUserData.setServerId(rs.getLong("serverId"));
        sonHaiUserData.setEnergy(rs.getInt("energy"));
        sonHaiUserData.setEnergyRestoreTime(rs.getLong("energyRestoreTime"));
        sonHaiUserData.setScore(rs.getLong("score"));
        sonHaiUserData.setAuraScore(rs.getLong("auraScore"));
        sonHaiUserData.setBuffComeNum(rs.getInt("buffComeNum"));
        sonHaiUserData.setNextBuffComeNeedEnergy(rs.getInt("nextBuffComeNeedEnergy"));
        if (!StringUtils.isNullOrEmpty(rs.getString("waitSelectBuffList"))) {
            sonHaiUserData.setWaitSelectBuffList(JSON.parseObject(rs.getString("waitSelectBuffList"), new TypeReference<List<SonHaiWaitSelectBuffData>>() {}));
        }
        if (!StringUtils.isNullOrEmpty(rs.getString("buffMap"))) {
            sonHaiUserData.setBuffMap(JSON.parseObject(rs.getString("buffMap"), new TypeReference<Map<Integer, SonHaiBuffData>>() {}));
        }
        sonHaiUserData.setRegionId(rs.getInt("regionId"));
        sonHaiUserData.setX(rs.getInt("x"));
        sonHaiUserData.setY(rs.getInt("y"));
        sonHaiUserData.setPvpKillNum(rs.getInt("pvpKillNum"));
        sonHaiUserData.setPvpCanBeAttNum(rs.getInt("pvpCanBeAttNum"));
        sonHaiUserData.setDieFlag(rs.getInt("dieFlag"));
        if (!StringUtils.isNullOrEmpty(rs.getString("dieData"))) {
            sonHaiUserData.setDieData(JSON.parseObject(rs.getString("dieData"), new TypeReference<SonHaiUserDieData>() {}));
        }
        sonHaiUserData.setTalentAddition(rs.getInt("talentAddition"));
        sonHaiUserData.setPower(rs.getLong("power"));
        sonHaiUserData.setAllBlood(rs.getLong("allBlood"));
        sonHaiUserData.setReBlood(rs.getLong("reBlood"));
        if (!StringUtils.isNullOrEmpty(rs.getString("reBloodInfoMap"))) {
            sonHaiUserData.setReBloodInfoMap(JSON.parseObject(rs.getString("reBloodInfoMap"), new TypeReference<Map<Long, SonHaiReBloodInfo>>() {}));
        }
        sonHaiUserData.setSelectRegionFlag(rs.getInt("selectRegionFlag"));
        sonHaiUserData.setHelpNum(rs.getInt("helpNum"));
        sonHaiUserData.setHelpNumCount(rs.getInt("helpNumCount"));
        sonHaiUserData.setLanguage(rs.getString("language"));
        sonHaiUserData.setCreateTime(rs.getLong("createTime"));
        return sonHaiUserData;
    }
}
